package com.alander.nexusgo.itinerary.service.impl;

import com.alander.nexusgo.common.model.dto.ItineraryDetailListDTO;
import com.alander.nexusgo.common.model.vo.ItineraryDetailVO;
import com.alander.nexusgo.itinerary.mapper.ItineraryDetailMapper;
import com.alander.nexusgo.itinerary.model.entity.ItineraryDetail;
import com.alander.nexusgo.itinerary.service.ItineraryDetailService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author alander
*/
@Service
public class ItineraryDetailServiceImpl extends ServiceImpl<ItineraryDetailMapper, ItineraryDetail>
    implements ItineraryDetailService {

    @Override
    public ItineraryDetailVO createItineraryDetail(ItineraryDetail itineraryDetail, Long userId) {
        // 这里可能需要检查用户是否有权限创建该行程详情
        this.save(itineraryDetail);
        return convertToVO(itineraryDetail);
    }

    @Override
    public ItineraryDetailVO updateItineraryDetail(Long id, ItineraryDetail itineraryDetail, Long userId) {
        ItineraryDetail existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("行程详情不存在");
        }

        // 这里可能需要检查用户是否有权限更新该行程详情
        itineraryDetail.setId(id);
        this.updateById(itineraryDetail);
        return convertToVO(this.getById(id));
    }

    @Override
    public boolean deleteItineraryDetail(Long id, Long userId) {
        ItineraryDetail existing = this.getById(id);
        if (existing == null) {
            throw new RuntimeException("行程详情不存在");
        }

        // 这里可能需要检查用户是否有权限删除该行程详情
        // 软删除
        LambdaUpdateWrapper<ItineraryDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ItineraryDetail::getId, id)
                .set(ItineraryDetail::getDelFlag, "1");
        return this.update(updateWrapper);
    }

    @Override
    public ItineraryDetailVO getItineraryDetailById(Long id) {
        ItineraryDetail itineraryDetail = this.getById(id);
        return itineraryDetail != null ? convertToVO(itineraryDetail) : null;
    }

    @Override
    public List<ItineraryDetailVO> listItineraryDetails(ItineraryDetailListDTO itineraryDetailListDTO) {
        LambdaQueryWrapper<ItineraryDetail> queryWrapper = new LambdaQueryWrapper<>();

        if (itineraryDetailListDTO.getItineraryId() != null) {
            queryWrapper.eq(ItineraryDetail::getItineraryId, itineraryDetailListDTO.getItineraryId());
        }

        if (itineraryDetailListDTO.getDayIndex() != null) {
            queryWrapper.eq(ItineraryDetail::getDayIndex, itineraryDetailListDTO.getDayIndex());
        }

        if (itineraryDetailListDTO.getItemType() != null && !itineraryDetailListDTO.getItemType().isEmpty()) {
            queryWrapper.eq(ItineraryDetail::getItemType, itineraryDetailListDTO.getItemType());
        }

        // 只查询未删除的行程详情
        queryWrapper.eq(ItineraryDetail::getDelFlag, "0");

        queryWrapper.orderByAsc(ItineraryDetail::getDayIndex)
                .orderByAsc(ItineraryDetail::getItemOrder);
        List<ItineraryDetail> itineraryDetailList = this.list(queryWrapper);

        return itineraryDetailList.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 将ItineraryDetail实体转换为ItineraryDetailVO
     * @param itineraryDetail ItineraryDetail实体
     * @return ItineraryDetailVO对象
     */
    private ItineraryDetailVO convertToVO(ItineraryDetail itineraryDetail) {
        ItineraryDetailVO itineraryDetailVO = new ItineraryDetailVO();
        BeanUtils.copyProperties(itineraryDetail, itineraryDetailVO);
        if (itineraryDetail.getStartTime() != null) {
            itineraryDetailVO.setStartTime(itineraryDetail.getStartTime().toString());
        }
        if (itineraryDetail.getEndTime() != null) {
            itineraryDetailVO.setEndTime(itineraryDetail.getEndTime().toString());
        }
        if (itineraryDetail.getCreateTime() != null) {
            itineraryDetailVO.setCreateTime(itineraryDetail.getCreateTime().toString());
        }
        if (itineraryDetail.getUpdateTime() != null) {
            itineraryDetailVO.setUpdateTime(itineraryDetail.getUpdateTime().toString());
        }
        return itineraryDetailVO;
    }
}